package com.cupcorn.lib.game;

import java.io.IOException;

import com.cupcorn.lib.define.GameEngineConfiguration;
import com.cupcorn.lib.game.element.ElementSound;

import org.andengine.audio.sound.Sound;
import org.andengine.audio.sound.SoundFactory;

public class Sfx
{
	GameEngine mainActivity;
	Sound player[];
	boolean isPaused[];
	boolean paused;
	
	public Sfx(GameEngine mainActivity, ElementSound[] CONTAINER)
	{
		player = new Sound[CONTAINER.length];
		isPaused = new boolean[CONTAINER.length];
		paused = false;
		for(int i = 0; i < player.length; i++)
		{
			try {
				player[i] = SoundFactory.createSoundFromAsset(mainActivity.getSoundManager(), mainActivity, CONTAINER[i].getPath());
				player[i].setLooping(CONTAINER[i].isLooping());
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public void play(int index)
	{
		if(!GameEngineConfiguration.use_sound)return;
		paused = false;
		player[index].play();
	}
	
	public void pause(int index)
	{
		if(!GameEngineConfiguration.use_sound)return;
		paused = true;
		player[index].pause();
	}
	
	public void pauseAll()
	{
		for(int i = 0; i < player.length; i++)
		{
			pause(i);
		}
	}
	
	public void Stop(int index)
	{
		if(!GameEngineConfiguration.use_sound)return;
		paused = false;
		player[index].stop();
	}
	
	public void resume(int index)
	{
		if(!GameEngineConfiguration.use_sound)return;
		if(paused)
		{
			paused = false;
			player[index].play();
		}
		else
		{
			System.out.println("cannot resume music...");
		}
	}
	
	public void resumeAll()
	{
		for(int i = 0; i < player.length; i++)
		{
			resume(i);
		}
	}
}
